package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.Vo.officialPostVo;
import cn.leyaber.hrm.clients.UserFeignClient;
import cn.leyaber.hrm.domain.OfficialPost;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.query.OfficialPostQuery;
import cn.leyaber.hrm.service.IOfficialPostService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@Component
@RestController
@RequestMapping("/officialPost")
public class OfficialPostController {
    @Autowired
    public IOfficialPostService officialPostService;
    @Autowired
    private UserFeignClient userFeignClient;

    /**
     * 批量删除
     * @param ids
     * @return
     */
   @PatchMapping(value="/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long>ids){
        try {
            officialPostService. batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
    /**
    * 保存和修改公用的
    * @param officialPost  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody OfficialPost officialPost){
        try {
            if(officialPost.getId()!=null){
                officialPostService.updateById(officialPost);
            }else{
                officialPostService.insert(officialPost);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            officialPostService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public OfficialPost get(@PathVariable("id")Long id)
    {
        return officialPostService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<OfficialPost> list(){

        return officialPostService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<officialPostVo> json(@RequestBody OfficialPostQuery query)
    {
        Page<OfficialPost> page = new Page<OfficialPost>(query.getPage(),query.getRows());
        page = officialPostService.selectPage(page);
        Page<officialPostVo> page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<officialPostVo> objects = new ArrayList<>();
        for (OfficialPost record : page.getRecords()) {
            officialPostVo officialPostVo = JSON.parseObject(JSON.toJSONString(record), officialPostVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())){
                objects.add(officialPostVo);
                continue;

            }
            long l = record.getUserWholenameId().longValue();
            Wholename wholename = userFeignClient.selectById(l);
            officialPostVo.setUser(wholename.getUser());
            objects.add(officialPostVo);
        }
        page1.setRecords(objects);
        return new PageList<officialPostVo>(page1.getTotal(),page1.getRecords());
    }
}
